package com.wq.wrongquestion.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageInfo;
import com.wq.wrongquestion.entity.che.ChemistryEntity;
import com.wq.wrongquestion.service.che.ChemistryService;
import com.wq.wrongquestion.utils.FileUploadUtils;

@Controller
@RequestMapping("/che")
public class ChemistryController {

    final String IMG_PAHT_YT_CHE = "images/che/yt/";
    final String IMG_PAHT_DA_CHE = "images/che/da/";

    @Autowired
    public ChemistryService chemistryService;

    @Autowired
    public FileUploadUtils fileUploadUtils;

   /************************************************Chemistry Start************************************************/
    @RequestMapping("/chemistryInit") 
    public String chemistryInit(Model model){
        //章节
        List<String> allZj=chemistryService.getChemistryZj();
        
        model.addAttribute("allZj", allZj);
        model.addAttribute("condEntity", new ChemistryEntity());
        
        return "che/chemistrySearch";
    }


    @PostMapping("/chemistryIns")
    public String insChemistry(ChemistryEntity chemistryEntity, @RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile, Model model) {
        // 获取文件名
        String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
        String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();

        // 获取文件路径
        String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_CHE);
        String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_CHE);

        // 保存文件
        try {
            if (ytFile.getSize() > 0) {
                fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                chemistryEntity.setImgytnm(fileName);
                chemistryEntity.setImgytpath(filePath);
            }
            if (daFile.getSize() > 0) {
                fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                chemistryEntity.setImgdanm(fileName2);
                chemistryEntity.setImgdapath(filePath2);
            }
            // 插入数据
            chemistryEntity.setFxcs(1);
            chemistryEntity.setXueke(chemistryEntity.getHidXueke());

            chemistryService.insChemistry(chemistryEntity);

            // return "redirect:/che/chemistryInit";
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "发生错误,请重试!");
            return "redirect:/che/addChemistry";
        }
        return "redirect:/che/chemistryInit";
    }
    @RequestMapping("/addChemistry")
    public String addChemistry(Model model){
        List<String> allZj=chemistryService.getChemistryZj();
        
        model.addAttribute("allZj", allZj);
        return "che/addChemistry";
    }

    @RequestMapping("/searchChemistry")
    public String searchChemistry(Model model, @RequestParam(value = "pageNm",defaultValue= "1") Integer pageNum,
            @RequestParam(value="pageSize" ,defaultValue= "10") Integer pageSize, ChemistryEntity chemistryEntity) {

        PageInfo<ChemistryEntity> chemistry = chemistryService.getChemistry(pageNum, pageSize, chemistryEntity);
        List<String> allZj=chemistryService.getChemistryZj();

        model.addAttribute("allZj", allZj);
        model.addAttribute("chemistry", chemistry);
        model.addAttribute("condEntity", chemistryEntity);

        return "che/chemistrySearch";
    }

    @RequestMapping("/searchChemistryByID")
    public String searchChemistryByID(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {

        List<ChemistryEntity> chemistryByID = chemistryService.getChemistryByID(id);
        List<String> allZj=chemistryService.getChemistryZj();
        if(!chemistryByID.isEmpty()){
            model.addAttribute("chemistry", chemistryByID);
        }else{
            model.addAttribute("msg", "这条记录在数据库中不存在！");
            return "che/chemistrySearch";
        }
        
        model.addAttribute("allZj", allZj);

        return "che/modifyChemistry";
    }

    @PostMapping("/chemistryUpd")
    public String chemistryUpd(Model model, ChemistryEntity chemistryEntity,@RequestParam("ytfileinfo") MultipartFile ytFile,
            @RequestParam("dafileinfo") MultipartFile daFile) {
        try {
            // 图片比较
            if(ytFile != null && ytFile.getOriginalFilename() !=null){
                if(!ytFile.getOriginalFilename().startsWith("yt", 0)){
                    // 获取文件名
                    String fileName = "yt" + System.currentTimeMillis() + "_" + ytFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath = fileUploadUtils.getUploadFilePath(IMG_PAHT_YT_CHE);
                    if (ytFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(ytFile.getBytes(), filePath, fileName);
                        chemistryEntity.setImgytnm(fileName);
                        chemistryEntity.setImgytpath(filePath);
                    }
                }
            }
            // 图片比较
            if(daFile != null && daFile.getOriginalFilename() !=null){
                if(!daFile.getOriginalFilename().startsWith("da", 0)){
                    // 获取文件名
                    String fileName2 = "da" + System.currentTimeMillis() + "_" + daFile.getOriginalFilename();
                    // 获取文件路径
                    String filePath2 = fileUploadUtils.getUploadFilePath(IMG_PAHT_DA_CHE);
                    if (daFile.getSize() > 0) {
                        fileUploadUtils.uploadFile(daFile.getBytes(), filePath2, fileName2);
                        chemistryEntity.setImgdanm(fileName2);
                        chemistryEntity.setImgdapath(filePath2);
                    }                
                }
            }
            // 插入数据
            chemistryEntity.setFxcs(1);
            chemistryEntity.setXueke(chemistryEntity.getHidXueke());

            Integer updRes=chemistryService.updChemistry(chemistryEntity);

            if(updRes==1){
                model.addAttribute("msg", "修改成功！");
            }else{
                model.addAttribute("msg", "修改失败！");
            } 
            List<ChemistryEntity> chemistryByID = chemistryService.getChemistryByID(chemistryEntity.getId());           
            if(chemistryByID.isEmpty()){
               chemistryByID = new ArrayList<ChemistryEntity>();
            }
            model.addAttribute("chemistry", chemistryByID);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            
            model.addAttribute("msg", "文件上传发生错误,请重试!");
            
        }


        return "che/modifyChemistry";   
    }

    @PostMapping("/chemistryDel")
    public String chemistryDel(Model model, @RequestParam(value = "pkey",defaultValue= "") Integer id) {
        chemistryService.delChemistry(id);
        
        return "redirect:/che/chemistryInit";
    }    
    /************************************************Chemistry End************************************************/
}
